/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.functions;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.jdiv.JDiv;
import com.jdiv.internalClasses.JDivFont;
import com.jdiv.kernel.JDivResources;

/** Class that creates and manages Texts.*/
public class FunctionsFont {
	 
	/**
	  * Load an Font from file.
	  * @param strFile File of font.
	  * @param strFilePng File of font "png".
	  * @return Number identifier of Font loaded, or null if not loaded.
	  */

	 public int loadFont( String strFile, String strFilePng )
	 {
		FileHandle file = Gdx.files.internal(strFile);
		
		if( !file.exists() ){
			JDiv.system.log("ERROR, loadFont() - File not found: '" + strFile + "'" );
			return 0;
		}
		
	    FileHandle filePng = Gdx.files.internal(strFilePng);
		
		if( !filePng.exists() ){
			JDiv.system.log("ERROR, loadFont() - File not found: '" + strFilePng + "'" );
			return 0;
		}
		
		return JDivResources.listFonts.fontAdd(file, filePng);	
	 }
	 
	 /**
	   * Unload Font stored in JDiv. Be careful when using this function, if you unload the font can no longer be reused.
	   * @param idFont Number identifier of Font.
	   */
	 
	 public void unloadFont( int idFont ){
		 JDivResources.listFonts.fontDispose(idFont);
	 }
	 
	 /**
	  * @param idFont Number identifier of Font.
	  * @return BitmapFont of LigGdx.
	  */
	 
	 public BitmapFont getFont( int idFont ){
		JDivFont jdivFont = JDivResources.listFonts.getJDivFont(idFont);
		
		BitmapFont bmpFont = null;
		
		if( jdivFont != null )
			bmpFont = jdivFont.getFont();
		
		if( bmpFont == null )
			JDiv.system.log("getFont() - Before using this function must call to 'frame()' so that the load is made.");
			
		return bmpFont;
	 }
	 
	 /**
	  * @param idFont Number identifier of Font.
	  * @return Space width of font.
	  */
	 
	 public float getSpaceWidth( int idFont ){
		 BitmapFont font = getFont( idFont );
		 
		 if( font != null ){
			 return font.getSpaceWidth();
		 }else
			 return 0;
	 }
	 
	 /**
	  * @param idFont Number identifier of Font.
	  * @return Cap height of font.
	  */
	 
	 public float getCapHeight( int idFont ){
         BitmapFont font = getFont( idFont );
		 
		 if( font != null ){
			 return font.getCapHeight();
		 }else
			 return 0;
	 }
	 
	 /** 
	  * @param idFont Number identifier of Font.
	  * @param string String of the text.
	  * @return Width the text of font;
	  */
	 
	 public float getWidthOfText( int idFont, String string ){
		 BitmapFont font = getFont( idFont );
		 
		 if( font != null ){
			 TextBounds textBounds = font.getBounds(string);
			 return textBounds.width;
		 }else
			 return 0;
	 }
	 
	 /** 
	  * @param idFont Number identifier of Font.
	  * @param string String of the text.
	  * @return Height the text of font;
	  */
	 
	 public float getHeightOfText( int idFont, String string ){
		 BitmapFont font = getFont( idFont );
		 
		 if( font != null ){
			 TextBounds textBounds = font.getBounds(string);
			 return textBounds.height;
		 }else
			 return 0;
	 }
	 
	 /**
	  * Eliminates all texts created by {@link com.jdiv.Write}
	  */
	 
	 public void deleteAllWrites( ){
	  JDivResources.listWrites.writeDeleteAll();	 
	 }
	 
}
